using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ICP_MS.Plugin.Tune.Properties;

namespace ICP_MS.Plugin.Tune;

public class SliderControl : UserControl
{
	private double max = 1000.0;

	private double _tipMin = double.NaN;

	private double _tipMax = double.NaN;

	private double _min = 500.0;

	private double _coe = 1.0;

	private double _value = 0.0;

	private double _minStep = 1.0;

	private IContainer components = null;

	private Button buttonRight;

	private Button buttonLeft;

	private ColorSlider colorSliderMs;

	private Label labelTX;

	private Button button1;

	private Button button2;

	[Description("最大值")]
	[Category("ColorSlider")]
	[DefaultValue(1000)]
	public double Max
	{
		get
		{
			return max;
		}
		set
		{
			max = value;
			colorSliderMs.Minimum = 0;
			colorSliderMs.Maximum = (int)Math.Round((max - _min) / _minStep, MidpointRounding.AwayFromZero);
		}
	}

	[Description("提示最小值")]
	[Category("ColorSlider")]
	[DefaultValue(-1)]
	public double TipMin
	{
		get
		{
			return _tipMin;
		}
		set
		{
			_tipMin = value;
			colorSliderMs.TipMin = (int)Math.Round((_tipMin / _coe - _min) / (max - _min) * (double)(colorSliderMs.Maximum - colorSliderMs.Minimum) + (double)colorSliderMs.Minimum, MidpointRounding.AwayFromZero);
		}
	}

	[Description("提示最大值")]
	[Category("ColorSlider")]
	[DefaultValue(-1)]
	public double TipMax
	{
		get
		{
			return _tipMax;
		}
		set
		{
			_tipMax = value;
			colorSliderMs.TipMax = (int)Math.Round((_tipMax / _coe - _min) / (max - _min) * (double)(colorSliderMs.Maximum - colorSliderMs.Minimum) + (double)colorSliderMs.Minimum, MidpointRounding.AwayFromZero);
		}
	}

	[Description("最小值")]
	[Category("ColorSlider")]
	[DefaultValue(500)]
	public double Min
	{
		get
		{
			return _min;
		}
		set
		{
			_min = value;
			colorSliderMs.Minimum = 0;
			colorSliderMs.Maximum = (int)Math.Round((max - _min) / _minStep, MidpointRounding.AwayFromZero);
		}
	}

	[Description("设置系数")]
	[Category("ColorSlider")]
	[DefaultValue(1)]
	public double Coe
	{
		get
		{
			return _coe;
		}
		set
		{
			_coe = value;
		}
	}

	[Description("Set Slider value")]
	[Category("ColorSlider")]
	[DefaultValue(800)]
	public double Value
	{
		get
		{
			return _value;
		}
		set
		{
			if (value < _min)
			{
				_value = _min;
			}
			else if (value > max)
			{
				_value = max;
			}
			else
			{
				_value = value;
			}
			colorSliderMs.Value = (int)Math.Round((_value / _coe - _min) / (max - _min) * (double)(colorSliderMs.Maximum - colorSliderMs.Minimum) + (double)colorSliderMs.Minimum, MidpointRounding.AwayFromZero);
			labelTX.Text = _value.ToString("f2");
			if (this.ValueChanged != null)
			{
				this.ValueChanged(this, new EventArgs());
			}
			Invalidate();
		}
	}

	[Description("最小步进")]
	[Category("ColorSlider")]
	[DefaultValue(1)]
	public double MinStep
	{
		get
		{
			return _minStep;
		}
		set
		{
			_minStep = value;
			colorSliderMs.Minimum = 0;
			colorSliderMs.Maximum = (int)Math.Round((max - _min) / _minStep, MidpointRounding.AwayFromZero);
		}
	}

	[Description("Event fires when the Value property changes")]
	[Category("Action")]
	public event EventHandler ValueChanged;

	[Description("Event fires when the Slider position is changed")]
	[Category("Behavior")]
	public new event ScrollEventHandler Scroll;

	public SliderControl()
	{
		InitializeComponent();
	}

	private void colorSlider1_ValueChanged(object sender, EventArgs e)
	{
		labelTX.Text = _value.ToString("f2");
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, e);
			double valueTemp = (double)(colorSliderMs.Value - colorSliderMs.Minimum) * (max - _min) / (double)(colorSliderMs.Maximum - colorSliderMs.Minimum) + _min;
			valueTemp = (double)(int)Math.Round(valueTemp / _minStep, MidpointRounding.AwayFromZero) * _minStep;
			_value = valueTemp * Coe;
		}
	}

	private void colorSlider1_Scroll(object sender, ScrollEventArgs e)
	{
		if (this.Scroll != null)
		{
			this.Scroll(this, e);
		}
	}

	private void buttonLeft_Click(object sender, EventArgs e)
	{
		colorSliderMs.ValueChanged -= colorSlider1_ValueChanged;
		double tempValue = _value - MinStep;
		if (!(tempValue >= _min))
		{
			tempValue = _min;
		}
		Value = tempValue;
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, e);
			colorSliderMs.ValueChanged += colorSlider1_ValueChanged;
		}
	}

	private void buttonRight_Click(object sender, EventArgs e)
	{
		colorSliderMs.ValueChanged -= colorSlider1_ValueChanged;
		double tempValue = _value + MinStep;
		if (!(tempValue <= max))
		{
			tempValue = max;
		}
		Value = tempValue;
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, e);
			colorSliderMs.ValueChanged += colorSlider1_ValueChanged;
		}
	}

	private void button1_Click(object sender, EventArgs e)
	{
		colorSliderMs.ValueChanged -= colorSlider1_ValueChanged;
		double tempValue = _value + 10.0 * MinStep;
		if (!(tempValue <= max))
		{
			tempValue = max;
		}
		Value = tempValue;
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, e);
			colorSliderMs.ValueChanged += colorSlider1_ValueChanged;
		}
	}

	private void button2_Click(object sender, EventArgs e)
	{
		colorSliderMs.ValueChanged -= colorSlider1_ValueChanged;
		double tempValue = _value - 10.0 * MinStep;
		if (!(tempValue <= max))
		{
			tempValue = max;
		}
		Value = tempValue;
		if (this.ValueChanged != null)
		{
			this.ValueChanged(this, e);
			colorSliderMs.ValueChanged += colorSlider1_ValueChanged;
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.labelTX = new System.Windows.Forms.Label();
		this.buttonRight = new System.Windows.Forms.Button();
		this.buttonLeft = new System.Windows.Forms.Button();
		this.button1 = new System.Windows.Forms.Button();
		this.button2 = new System.Windows.Forms.Button();
		this.colorSliderMs = new ICP_MS.Plugin.Tune.ColorSlider();
		base.SuspendLayout();
		this.labelTX.AutoSize = true;
		this.labelTX.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		this.labelTX.ForeColor = System.Drawing.Color.DodgerBlue;
		this.labelTX.Location = new System.Drawing.Point(235, 6);
		this.labelTX.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.labelTX.Name = "labelTX";
		this.labelTX.Size = new System.Drawing.Size(55, 15);
		this.labelTX.TabIndex = 14;
		this.labelTX.Text = "label1";
		this.buttonRight.Image = ICP_MS.Plugin.Tune.Properties.Resources.Right;
		this.buttonRight.Location = new System.Drawing.Point(193, 5);
		this.buttonRight.Margin = new System.Windows.Forms.Padding(4);
		this.buttonRight.Name = "buttonRight";
		this.buttonRight.Size = new System.Drawing.Size(18, 21);
		this.buttonRight.TabIndex = 12;
		this.buttonRight.UseVisualStyleBackColor = true;
		this.buttonRight.Click += new System.EventHandler(buttonRight_Click);
		this.buttonLeft.Image = ICP_MS.Plugin.Tune.Properties.Resources.left;
		this.buttonLeft.Location = new System.Drawing.Point(25, 5);
		this.buttonLeft.Margin = new System.Windows.Forms.Padding(4);
		this.buttonLeft.Name = "buttonLeft";
		this.buttonLeft.Size = new System.Drawing.Size(18, 21);
		this.buttonLeft.TabIndex = 11;
		this.buttonLeft.UseVisualStyleBackColor = true;
		this.buttonLeft.Click += new System.EventHandler(buttonLeft_Click);
		this.button1.Image = ICP_MS.Plugin.Tune.Properties.Resources.Right2;
		this.button1.Location = new System.Drawing.Point(213, 5);
		this.button1.Margin = new System.Windows.Forms.Padding(4);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(18, 21);
		this.button1.TabIndex = 12;
		this.button1.UseVisualStyleBackColor = true;
		this.button1.Click += new System.EventHandler(button1_Click);
		this.button2.Image = ICP_MS.Plugin.Tune.Properties.Resources.left1;
		this.button2.Location = new System.Drawing.Point(6, 5);
		this.button2.Margin = new System.Windows.Forms.Padding(4);
		this.button2.Name = "button2";
		this.button2.Size = new System.Drawing.Size(18, 21);
		this.button2.TabIndex = 12;
		this.button2.UseVisualStyleBackColor = true;
		this.button2.Click += new System.EventHandler(button2_Click);
		this.colorSliderMs.BackColor = System.Drawing.Color.Transparent;
		this.colorSliderMs.BarInnerColor = System.Drawing.Color.SkyBlue;
		this.colorSliderMs.BorderRoundRectSize = new System.Drawing.Size(8, 8);
		this.colorSliderMs.ElapsedInnerColor = System.Drawing.Color.Blue;
		this.colorSliderMs.ElapsedOuterColor = System.Drawing.Color.Blue;
		this.colorSliderMs.LargeChange = 5u;
		this.colorSliderMs.Location = new System.Drawing.Point(45, 6);
		this.colorSliderMs.Margin = new System.Windows.Forms.Padding(4);
		this.colorSliderMs.Maximum = 200;
		this.colorSliderMs.Name = "colorSliderMs";
		this.colorSliderMs.Size = new System.Drawing.Size(145, 18);
		this.colorSliderMs.SmallChange = 1u;
		this.colorSliderMs.TabIndex = 13;
		this.colorSliderMs.Text = "colorSlider1";
		this.colorSliderMs.ThumbPenColor = System.Drawing.Color.Black;
		this.colorSliderMs.ThumbRoundRectSize = new System.Drawing.Size(8, 8);
		this.colorSliderMs.ThumbSize = 8;
		this.colorSliderMs.TipMax = 80;
		this.colorSliderMs.ValueChanged += new System.EventHandler(colorSlider1_ValueChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.labelTX);
		base.Controls.Add(this.colorSliderMs);
		base.Controls.Add(this.button2);
		base.Controls.Add(this.button1);
		base.Controls.Add(this.buttonRight);
		base.Controls.Add(this.buttonLeft);
		base.Margin = new System.Windows.Forms.Padding(4);
		base.Name = "SliderControl";
		base.Size = new System.Drawing.Size(326, 30);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
